$(function() {
	var seekerPanel = $('#seeker-panel'),
		seekerBody = seekerPanel.find('.panel-body'),
		seekerListDiv = seekerPanel.find('tbody'),

		companyPanel = $('#company-panel'),
		companyBody = companyPanel.find('.panel-body'),
		companyListDiv = companyPanel.find('tbody'),

		seekerModal = $('#seeker-modal'),
		companyModal = $('#company-modal');

	var workageEnum = ['应届生', '1年', '2年', '3年', '4年', '5年', '5年以上'],
		degreeEnum = ['大专', '本科', '硕士', '博士'],
		sexEnum = ['男', '女'];

	var dataStorage = {
		seeker: {
			action: undefined,
			current: undefined
		},
		company: {
			action: undefined,
			current: undefined
		}
	};

	seekerBody.height($(window).height() - 251);
	companyBody.height($(window).height() - 250);
	$(window).resize(function() {
	    seekerBody.height(Math.max($(this).height() - 250, 500));
	    companyBody.height(Math.max($(this).height() - 250, 500));
	});

	$.get(DOMAIN_PATH + '/seeker/list', function(list) {
		list.forEach(function(seeker) {
			seekerListDiv.append(constructSeekerDiv(seeker));
		});
	});

	$.get(DOMAIN_PATH + '/company/list', function(list) {
		list.forEach(function(company) {
			companyListDiv.append(constructCompanyDiv(company));
		});
	});


	$('#seeker').on('click', function(e) {
		e.preventDefault();
		companyPanel.hide();
		seekerPanel.show();
	});

	$('#company').on('click', function(e) {
		e.preventDefault();
		companyPanel.show();
		seekerPanel.hide();
	});

	$('#seeker-add').on('click', function() {
		seekerModal.find('form')[0].reset();
		seekerModal.find('.alert').hide();
		seekerModal.modal('show');
		dataStorage.seeker.action = 'add';
	});

	seekerModal.find('.btn-primary').on('click', function() {
		var inputDivs = seekerModal.find('input'),
            selectDivs = seekerModal.find('select');
        var param = {
            name: inputDivs[0].value,
            pwd: inputDivs[1].value,

            sex: selectDivs[0].value,

            birthday: inputDivs[2].value,
            school: inputDivs[3].value,
            major: inputDivs[4].value,

            degree: selectDivs[1].value,
            workage: selectDivs[2].value,

            phone: inputDivs[5].value,
            mail: inputDivs[6].value,
            city: inputDivs[7].value
        };

        for (var key in param) {
            if (param[key].length == 0) {
            	seekerModal.find('.alert').text('缺少 ' + key + ' 信息').show();
                return;
            }
        }

        if (dataStorage.seeker.action == 'add') {
	        $.post(DOMAIN_PATH + '/seeker', param, function(result) {
	        	if (result.succeeded) {
	        		seekerListDiv.prepend(constructSeekerDiv(result.obj));
	        		seekerModal.modal('hide');
	        	} else {
	        		seekerModal.find('.alert').text('Add failed').show();
	        	}
	        });
        } else if (dataStorage.seeker.action == 'edit') {
        	$.post(DOMAIN_PATH + '/seeker/id/' + dataStorage.seeker.current.data('id'), param, function(result) {
        		if (result.succeeded) {
        			var div = dataStorage.seeker.current;
        			div.find('td:eq(0)').text(result.obj.name);
        			div.find('td:eq(1)').text(sexEnum[result.obj.sex]);
        			div.find('td:eq(2)').text(result.obj.birthday);
        			div.find('td:eq(3)').text(result.obj.school);
        			div.find('td:eq(4)').text(result.obj.major);
        			div.find('td:eq(5)').text(degreeEnum[result.obj.degree]);
        			div.find('td:eq(6)').text(workageEnum[result.obj.workage]);
        			div.find('td:eq(7)').text(result.obj.phone);
        			div.find('td:eq(8)').text(result.obj.mail);
        			div.find('td:eq(9)').text(result.obj.city);

		            seekerModal.modal('hide');
        		} else {
	        		seekerModal.find('.alert').text('Add failed').show();
	        	}
        	});
        }
	});

	function constructSeekerDiv(seeker) {
		var div = $('<tr data-id="' + seeker.id + '">' + 
                        '<td>' + seeker.name + '</td>' +
                        '<td>' + sexEnum[seeker.sex] + '</td>' +
                        '<td>' + seeker.birthday + '</td>' +
                        '<td>' + seeker.school + '</td>' +
                        '<td>' + seeker.major + '</td>' +
                        '<td>' + degreeEnum[seeker.degree] + '</td>' +
                        '<td>' + workageEnum[seeker.workage] + '</td>' +
                        '<td>' + seeker.phone + '</td>' +
                        '<td>' + seeker.mail + '</td>' +
                        '<td>' + seeker.city + '</td>' +
                        '<td><button class="btn btn-primary btn-xs">Edit</button></td>' +
                        '<td><button class="btn btn-danger btn-xs">Delete</button></td>' +
                    '</tr>');
		
		div.find('.btn-primary').on('click', function() {
			seekerModal.find('.alert').hide();
			$.get(DOMAIN_PATH + '/seeker/id/' + seeker.id, function(result) {
				seekerModal.find('input:eq(0)').val(result.name);
				seekerModal.find('input:eq(1)').val(result.pwd);

	            seekerModal.find('select:eq(0)').val(result.sex);

	            seekerModal.find('input:eq(2)').val(result.birthday);
	            seekerModal.find('input:eq(3)').val(result.school);
	            seekerModal.find('input:eq(4)').val(result.major);

	            seekerModal.find('select:eq(1)').val(result.degree);
	            seekerModal.find('select:eq(2)').val(result.workage);

	            seekerModal.find('input:eq(5)').val(result.phone);
	            seekerModal.find('input:eq(6)').val(result.mail);
	            seekerModal.find('input:eq(7)').val(result.city);
				seekerModal.modal('show');
				dataStorage.seeker.action = 'edit';
				dataStorage.seeker.current = div;
			});
		});

		div.find('.btn-danger').on('click', function() {
			$.ajax({
				url: DOMAIN_PATH + '/seeker/id/' + seeker.id,
				type: 'DELETE',
			}).done(function(result) {
				if (result.succeeded) {
					div.remove();
				}
			});
		});

		return div;
	}

	$('#company-add').on('click', function() {
		companyModal.find('form')[0].reset();
		companyModal.find('.alert').hide();
		companyModal.modal();
		dataStorage.company.action = 'add';
	});

	companyModal.find('.btn-primary').on('click', function() {
		var param = {
			name: companyModal.find('input:eq(0)').val(),
			pwd: companyModal.find('input:eq(1)').val(),
			address: companyModal.find('input:eq(2)').val(),
			phone: companyModal.find('input:eq(3)').val(),
			website: companyModal.find('input:eq(4)').val()
		};
		
        for (var key in param) {
            if (param[key].length == 0) {
            	companyModal.find('.alert').text('缺少 ' + key + ' 信息').show();
                return;
            }
        }
        if (dataStorage.company.action == 'add') {
	        $.post(DOMAIN_PATH + '/company', param, function(result) {
	        	if (result.succeeded) {
	        		companyListDiv.prepend(constructCompanyDiv(result.obj));
	        		companyModal.modal('hide');
	        	} else {
	        		companyModal.find('.alert').text('Add failed').show();
	        	}
	        });
        } else if (dataStorage.company.action == 'edit') {
        	$.post(DOMAIN_PATH + '/company/id/' + dataStorage.company.current.data('id'), param, function(result) {
        		if (result.succeeded) {
        			var div = dataStorage.company.current;
        			div.find('td:eq(0)').text(result.obj.name);
        			div.find('td:eq(1)').text(result.obj.address);
        			div.find('td:eq(2)').text(result.obj.phone);
        			div.find('td:eq(3)').text(result.obj.website);

	        		companyModal.modal('hide');
	        	} else {
	        		companyModal.find('.alert').text('Add failed').show();
	        	}
        	})
        }
	});

	function constructCompanyDiv(company) {
		var div = $('<tr data-id="' + company.id + '">' + 
                        '<td>' + company.name + '</td>' +
                        '<td>' + company.address + '</td>' +
                        '<td>' + company.phone + '</td>' +
                        '<td>' + company.website + '</td>' +
                        '<td><button class="btn btn-primary btn-xs">Edit</button></td>' +
                        '<td><button class="btn btn-danger btn-xs">Delete</button></td>' +
                    '</tr>');

		div.find('.btn-primary').on('click', function() {
			companyModal.find('.alert').hide();
			$.get(DOMAIN_PATH + '/company/id/' + company.id, function(result) {
				companyModal.find('input:eq(0)').val(result.name);
	            companyModal.find('input:eq(1)').val(result.pwd);
	            companyModal.find('input:eq(2)').val(result.address);
	            companyModal.find('input:eq(3)').val(result.phone);
	            companyModal.find('input:eq(3)').val(result.website);
				companyModal.modal('show');
				dataStorage.company.action = 'edit';
				dataStorage.company.current = div;
			});
		});

		div.find('.btn-danger').on('click', function() {
			$.ajax({
				url: DOMAIN_PATH + '/company/id/' + company.id,
				type: 'DELETE',
			}).done(function(result) {
				if (result.succeeded) {
					div.remove();
				}
			});
		});

		return div;
	}

});